package com.ishell.generate.util;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

/**
 * 作者: creazier.huang 日期: 2017/1/10 说明:
 */
public class PropertyUtil {
    private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
    private static Properties p = new Properties();
    /**
     * 读取properties配置文件信息
     */
    static{
        try {
            InputStream in = PropertyUtil.class.getResourceAsStream("/jdbc.properties");
            p.load(new InputStreamReader(in, "UTF-8"));
        } catch (Exception e) {
            logger.error("初始化失败，无法读取配置文件", e);
            throw new RuntimeException("初始化失败，无法读取配置文件", e);
        }
    }



    /**
     * 根据key得到value的值
     */
    public static String getValue(String key)
    {
        return p.getProperty(key);
    }

    public static List<String> getList(String key){
        String value = p.getProperty(key);
        if(StringUtils.isEmpty(value)) return null;
        List<String> list = Arrays.asList(value.split(","));
        return list;
    }

    public static String getValue(String key, String defaultValue) {
        return p.getProperty(key, defaultValue);
    }
}
